// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class DBLItem extends cc.Component {
    
    public static ID_1:number = 1;
    public static ID_2:number = 2;
    public static ID_3:number = 3;

    public static ID_BING:number = 4;
    public static ID_RMB:number = 5;
    public static ID_CHAI:number = 6;

    private gameComponent:any=null;

    private ready:boolean = false;
    
    public hasRemoved:boolean = false;


    @property
    row:number = 0;

    @property
    col:number = 0;

    @property(cc.Label)
    label: cc.Label = null;

    @property
    id:number=0;

    //3个合成的
    @property(cc.Node)
    item1:cc.Node=null;//  1/4菠萝
    @property(cc.Node)
    item2:cc.Node=null;//  1/2菠萝
    @property(cc.Node)
    item3:cc.Node=null;//  1菠萝

    @property(cc.Node)
    itemBing:cc.Node=null;
    @property(cc.Node)
    itemChai:cc.Node=null;
    @property(cc.Node)
    itemJing:cc.Node=null;

    @property(cc.Node)
    itemRMB:cc.Node=null;        
    // LIFE-CYCLE CALLBACKS:


    @property
    iscale:number = 0;
    @property
    iopacity:number = 0;


    //当前显示的图
    public curShowItem:cc.Node;

    //-----------------------------------------------------
    //移动相关变量
    //-----------------------------------------------------
    private moveFinishRange:number = 60;   //有效的移动距离
    private moveCheck:boolean = false;   //有效的移动距离
    //private moveChecking:boolean = false;   //有效的移动距离

    onLoad () {
    	//监听触摸开始事件
    	this.node.on(cc.Node.EventType.TOUCH_START,this.on_touch_start,this);
    	//监听触摸移动事件
    	//使用自定义回调函数
    	this.node.on(cc.Node.EventType.TOUCH_MOVE,this.on_touch_move,this);
    	//结束触摸移动事件
    	//this.node.off(cc.Node.EventType.TOUCH_MOVE,this.on_touch_move,this);
    	
    	//监听作用域内触摸抬起事件
    	this.node.on(cc.Node.EventType.TOUCH_END,this.on_touch_end,this);
    	//监听作用域外触摸抬起事件
    	this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.on_touch_cancel,this);

    }


















    private on_touch_start(t:any){
        if(this.gameComponent.isStopUpate()){
            this.moveCheck=false;  
            return;
        }

        if(this.id>DBLItem.ID_BING){
            if(this.id==DBLItem.ID_CHAI &&  this.curShowItem==this.itemChai){
                //TODO 是否看广告 ！！！！
                this.gameComponent.startWatchAdAction(this.node);
            }
            this.moveCheck=false;  
            return;
        }
        //函数体内写事件发生时的事情
        //当触摸开始是打印以下字样
        //console.log("触摸开始");
        this.moveCheck=true;        
    }
    private on_touch_end(t:any){
        //console.log("触摸内结束");
        this.moveCheck=false;      
    }
    private on_touch_cancel(t:any){
        //console.log("触摸外开始");
        this.moveCheck=false;  
    }    
    //自定义回调函数,参数t
    private on_touch_move(t:any){
        if(!this.moveCheck){
            return;
        }

        if(this.id>DBLItem.ID_BING){
            return;
        }

    	//定义一个n_pos变量存储当前触摸点的位置
    	var n_pos:any = t.getLocation();
    	//打印触摸点的坐标，x坐标，y坐标
    	//console.log(n_pos,n_pos.x,n_pos.y);

    	//定义变量delta存储变化距离
    	//var delta:any = t.getDelta();
    	//变化当前节点位置使其跟随触摸点,实现按住移动效果
    	//this.node.x += delta.x;
        //this.node.y += delta.y;
        
       

        //定义变量start_pos初始时的位置对象，对象包含 x 和 y 属性
    	var start_pos:any = t.getStartLocation();

        let distance = start_pos.sub(n_pos).mag();
        //console.log("移动距离："+distance);
        if(distance>=this.moveFinishRange){

           // console.log("移动 id： "+this.id+" row: "+this.row+" col: "+this.col);

            this.moveCheck=false;
            //this.moveChecking=true;

            let endX = start_pos.x - n_pos.x;
            let endY = start_pos.y - n_pos.y;    
            
            //1. 向左移动
            //2. 向右移动
            //3. 向上移动
            //4. 向下移动
            let direction:number = 0;

            if (Math.abs(endX) > Math.abs(endY)){
                //手势向左右
                //判断向左还是向右 
                if (endX  > 0){
                    //向左函数
                    //console.log('left');
                    if(this.col!=1){
                        direction=1;
                    }
                } else {
                    //向右函数
                    //console.log('right');
                    if(this.col!=4){
                        direction=2;
                    }
                }
            } else {
                //手势向上下
                //判断手势向上还是向下
                if (endY  > 0){
                    //向下函数
                    //console.log('down');
                    if(this.row>1){
                        direction=4;
                    }
                } else {
                    //向上函数
                    //console.log('up');
                    direction=3;
                }
             }  

             let tox:number = 0;
             let toy:number = 0;
            if(direction>0){
                let toRow:number = this.row;
                let toCol:number = this.col;
                switch(direction){
                    case 1:
                        toCol--;
                        tox = this.node.width * -1;
                        break;
                    case 2:
                        toCol++;
                        tox = this.node.width;
                        break;
                    case 3:
                        toRow++;
                        toy = this.node.height;
                        break;
                    default:
                        toRow--;
                        toy = this.node.height * -1;
                        break;                        
                }
                let mergeItem:any = this.gameComponent.checkMerge(this.id,toRow,toCol);
                if(mergeItem!=null){

                    //移动出去合并  效果
                    cc.tween(this.curShowItem)
                    .to(0.1, { position: cc.v3(tox, toy) })
                    .call(() => {
                        if(this.id==DBLItem.ID_3){
                            this.gameComponent.doMargeChainAction(this.node,mergeItem);
                        }else if(this.id==DBLItem.ID_BING){
                            //全屏炸
                            this.gameComponent.doBombAction();
                        }else{
                            if(direction==4){//向下移动合并
                                //mergeItem.destroy(); 
                                this.gameComponent.doMergeItemAction(this.node,toCol,this.row-2,mergeItem);
                            }else{
                                //this.node.destroy();
                                if(direction==3){
                                    this.gameComponent.doMergeItemAction(mergeItem,toCol,this.row-1,this.node);
                                }else{
                                    this.gameComponent.doMergeItemAction(mergeItem,this.col,mergeItem.getComponent("DBLItem").row-1,this.node);
                                }
                            }
                        }
                    })
                    .start();

                }else{
           
                    //移动回来  效果
                    cc.tween(this.curShowItem)
                    .to(0.05, { position: cc.v3(tox, toy) })
                    .to(0.05, { position: cc.v3(0, 0) })
                    .start();
             
                }
            } 
        }
    }

















    private showItemByID(){
        this.item1.active = false;
        this.item2.active = false;
        this.item3.active = false;
        this.itemBing.active = false;
        this.itemChai.active = false;
        this.itemJing.active = false;
        this.itemRMB.active = false;

        switch(this.id){
            case DBLItem.ID_1:
                this.item1.active = true;
                this.curShowItem=this.item1;
                break;
            case DBLItem.ID_2:
                this.item2.active = true;
                this.curShowItem=this.item2;
                break;                
            case DBLItem.ID_3:
                this.item3.active = true;
                this.curShowItem=this.item3;
                break;
            case DBLItem.ID_BING:
                this.itemBing.active = true;
                this.curShowItem=this.itemBing;
                break;        
            case DBLItem.ID_CHAI:
                this.itemChai.active = true;
                this.curShowItem=this.itemChai;
                break;
            case DBLItem.ID_RMB:
                this.itemRMB.active = true;
                this.curShowItem=this.itemRMB;
                break;                           
        }

        this.label.string = this.node.uuid.substring(4,this.node.uuid.length);
        //this.label.string = this.id+"-"+this.col+"-"+this.row
        //this.label.string = this.node.y.toFixed(2)+""

        
        // let Rand:number = Math.random(); //获取[0-1）的随机数
        // let rt:number = (1 + Math.round(Rand * 10)); //放大取整
        // rt = rt / 100;
        // cc.tween(this.node)
        // .delay(rt)
        // .call(() => {
        //     this.curShowItem.getComponent(sp.Skeleton).setAnimation(0, "action", true); // 把管道清空，加入当前这个，
        // })
        // .start();    
    }








    public initView(data: any) {
        this.id = data.id;
        this.row = data.row;
        this.col = data.col;
        this.gameComponent=data.gameComponent;
        
        this.showItemByID();
    }
    public updateView(data: any) {
        this.id = data.id;
        this.row = data.row;
        this.col = data.col;
        this.gameComponent=data.gameComponent;
        this.showItemByID();
    }












    update (dt:Number) {
        
        if( this.gameComponent==null || this.gameComponent.gameOver || this.gameComponent.stopUpate){
            return;
        }

        //每帧移动的距离
        let tmpMoveNum:number = this.gameComponent.getMoveStep();
        this.node.y = this.node.y - tmpMoveNum;
        //移动超出
        if(this.node.y<this.gameComponent.maxy){
            //this.gameComponent.checkPostion = true;
            this.gameComponent.gameOver=true;
        }
        //移动进屏幕
        if(this.node.y<this.gameComponent.outScreenY - this.gameComponent.spH/2/2 ){
            if(this.iscale==0){
                this.iscale = 1;
                this.iopacity = 255;

                this.curShowItem.scale=0;
                this.curShowItem.opacity=0;

                if(this.id==DBLItem.ID_CHAI){
                    cc.tween(this.curShowItem)
                    .to(0.1, {opacity: 255,scale:1})
                    .delay(3)
                    
                    .repeat(20,
                    cc.tween().to(0.1, {opacity: 100,scale:0.8})
                                .to(0.1, {opacity: 255,scale:1})
                    )   
                    .call(() => { 
                        this.itemChai.active = false;
                        this.itemJing.active = true;
                        this.curShowItem=this.itemJing;
                    })
                    .start()                
                }else{
                    cc.tween(this.curShowItem)
                    .to(0.1, {opacity: 255,scale:1})
                    .start();    
                }

            }
        }else{
            this.iscale = 0;
            this.iopacity = 0;
            this.curShowItem.scale=0;
            this.curShowItem.opacity=0;            
        }
        
        this.label.string = this.node.uuid.substring(4,this.node.uuid.length);
        //this.label.string = this.id+"-"+this.col+"-"+this.row
        //this.label.string = this.node.y.toFixed(2)+""
    }
}
